package com.koko.listener;

import com.koko.feign.ThirdPartyFeignService;
import com.koko.order.entity.Order;
import com.koko.order.vo.OrderVO;
import com.koko.properties.RabbitmqProperties;
import com.koko.service.UserService;
import com.koko.user.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Slf4j
@Component
public class SpringRabbitListener {
    @Autowired
    private UserService userService;

    @Autowired
    private ThirdPartyFeignService thirdPartyFeignService;

    @RabbitListener(queues = RabbitmqProperties.PAY_SUCCESS_USER_QUEUE)
    public void listenPaySuccessUserQueue(Order order) {
        log.info("user模块接收到" + RabbitmqProperties.PAY_SUCCESS_USER_QUEUE + "的消息：【" + order + "】");
        // 用户游戏关系包完善
        userService.getGamesFromOrder(order.getId());
    }

    @RabbitListener(queues = RabbitmqProperties.PAY_SUCCESS_NOTIFY_QUEUE)
    public void listenPaySuccessNotifyQueue(Order order) {
        log.info("user模块接收到" + RabbitmqProperties.PAY_SUCCESS_NOTIFY_QUEUE + "的消息：【" + order + "】");
        // 通知用户购买成功
        UserVO user = userService.getUserById(order.getUserId());
        Map<String, Object> content = new HashMap<>();
        content.put("user", user);
        content.put("order", order);
        thirdPartyFeignService.sendPaySuccessMessageToUser(content);
    }
}
